<?php
/**
 * 社区管理
 * @author Administrator
 *
 */
class communityManage extends CControllerManageBase{
	public function show()
	{
		$model = new communityModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		
		$where = "del_flag=0";

		$province = $this->getRequestData("sel_province","get");
		$city = $this->getRequestData("sel_city","get");
		$area = $this->getRequestData("sel_area","get");
		$community_name = $this->getRequestData("txt_community_name","get");

		$urlParam = array();
		
		if($province)
		{
			$where .= " and province='".$province."'";
			
			$urlParam["sel_province"] = $province;
		}
		
		if($city)
		{
			$where .= " and city='".$city."'";
			
			$urlParam["sel_city"] = $city;
		}
		
		if($area)
		{
			$where .= " and area='".$area."'";
			
			$urlParam["sel_area"] = $area;
		}
		
		if($community_name)
		{
			$where .= " and community_name like '%".$community_name."%'";
			
			$urlParam["txt_community_name"] = $community_name;
		}
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_community_name"));
		
		$list = $model->selectPage($pageIndex,$pageSize,"*",$where,"community_id desc","",$c);
		
		$pageUrl = $pageUrl? "?".$pageUrl:"";

		$pageStr = CPage::showPage($pageIndex,$pageSize,$c, $pageUrl);

		$this->setMenu("社区列表");
		$this->setJsBlock("init('".$province."','".$city."','".$area."',1);");
		$this->render("community/list",array("list"=>$list,"pageStr"=>$pageStr, "community_name"=>$community_name));
	}
	
	private function getPostInfo()
	{
		$modelInfo["community_name"] = $this->getRequestData("txt_community_name");
		$modelInfo["province"] = $this->getRequestData("sel_province");
		$modelInfo["city"] = $this->getRequestData("sel_city");
		$modelInfo["area"] = $this->getRequestData("sel_area");
		$modelInfo["address"] = $this->getRequestData("txt_address");
		$modelInfo["contact"] = $this->getRequestData("txt_contact");
		$modelInfo["phone"] = $this->getRequestData("txt_phone");
		$modelInfo["postcode"] = $this->getRequestData("txt_postcode");
		$modelInfo["summary"] = $this->getRequestData("txt_summary");
		
		return $modelInfo;
	}
	
 	public function add(){
		$errorStr="";
		$rule=CCustomFormValidator::communityValidator();
		$this->formValidator->init(array("formID"=>"t_communityForm"));
		$this->formValidator->setRules($rule);
		if($_POST){
			$modelInfo = $this->getPostInfo();
			$modelInfo["create_time"] = date('Y-m-d H:i:s');//创建时间
			
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new communityModel();
				
				$cid=$model->insert($modelInfo);
				
				if($cid>0)
				{
					$this->sysMsg("新增成功",0,array(array("text"=>"返回社区列表","href"=>$this->_manageDomain."communityManage/show/"),array("text"=>"继续新增","href"=>$this->_manageDomain."communityManage/add/")));
				}
				else
				{
					$this->sysMsg("新增失败",1);
				}
			}
		}
		
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setJsBlock("init();");
		$this->setMenu(array(array("text"=>"社区列表","href"=>$this->_manageDomain."communityManage/show/"),array("text"=>"社区新增")));
		$this->render("community/add",array("error"=>$errorStr));
	}

	public function edit(){
		$community_id = intval($this->getRequestData("id","GET"));
		if(!$community_id)
		{
			echo "参数错误";
			exit;
		}
		$modelInfo = array();
		$errorStr = "";

		$rule=CCustomFormValidator::communityValidator();
		$this->formValidator->init(array("formID"=>"t_communityForm"));
		$this->formValidator->setRules($rule);

		$model=new communityModel();
		if($_POST){
			$modelInfo = $this->getPostInfo();
			$modelInfo["community_id"] = $community_id;
			
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model->updateByPkId($modelInfo);
		        
		        header("location:".$this->_manageDomain."communityManage/show/");
		        exit();
			}
		}
		else{
			$modelInfo = $model->selectById($community_id,"del_flag=0");
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
			
			$this->setJsBlock("init('".$modelInfo["province"]."','".$modelInfo["city"]."','".$modelInfo["area"]."');");
		}
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"社区列表","href"=>$this->_manageDomain."communityManage/show/"),array("text"=>"社区修改")));
		$this->render("community/edit",array("error"=>$errorStr,"modelInfo"=>$modelInfo));
	}
	
	public function del()
	{
		$id = $this->getRequestData("id","GET");
		if ($id)
        {
            $obj = new communityModel();

			$obj->deleteByPkId($id);
            $this->show();
        }
	}
	
	public function batchDel()
	{
		$ids = $this->getRequestData("ids","GET");
	
		$idsList = explode(",",$ids);
	
		$buc = new communityModel();
		foreach($idsList as $id)
		{
			$buc->deleteByPkId($id);
		}
		$this->show();
	}
	
	public function getCommunityByCity()
	{
		$province = $this->getRequestData("province");
		$city = $this->getRequestData("city");
		$area = $this->getRequestData("area");
		$model=new communityModel();

		$clist = $model->getCommunityByArea($province, $city, $area);
		
		echo json_encode($clist);
	}
}